package com.yanyu.space.sys.bean.enums;

import cn.hutool.core.util.RandomUtil;
import com.yanyu.space.common.core.bean.enums.IRedisKey;
import org.apache.commons.lang3.StringUtils;

import java.util.concurrent.TimeUnit;

/**
 * @author yanyu
 */
public enum SysRedisKeyEnum implements IRedisKey {

    // 登录验证码
    IMAGECODE("sys:login:imagecode", 30L, TimeUnit.MINUTES),
    // 登录用户信息
    LOGIN_USER("sys:login:user", 1L, TimeUnit.HOURS),
    // 当前用户关联的角色信息
    ROLE_OF_USER("sys:user:role", 10L, TimeUnit.MINUTES),
    // 当前用户关联的菜单信息
    MENU_OF_USER("sys:user:menu", 10L, TimeUnit.MINUTES),
    ;
    SysRedisKeyEnum(String code, Long timeout, TimeUnit unit) {
        this.code = code;
        this.timeout = timeout;
        this.unit = unit;
    }

    private String code;

    private Long timeout;

    private TimeUnit unit;

    @Override
    public Long getTimeOut(int limit) {
        return timeout + RandomUtil.randomInt(limit);
    }

    @Override
    public TimeUnit getUnit() {
        return unit;
    }

    @Override
    public String getCode(Object... subfixCodes) {
        StringBuffer sb = new StringBuffer(this.code);
        if (subfixCodes.length > 0) {
            for (Object subfixCode : subfixCodes) {
                sb.append(":").append(subfixCode);
            }
            StringUtils.substringBeforeLast(sb.toString(), ":");
        }
        return sb.toString();
    }
}
